from pony import orm
from datetime import datetime
import pytz

from .config import settings


# orm.set_sql_debug(True)


db = orm.Database()
db.bind(
    provider='postgres',
    host=settings.PG_HOST_ZZ,
    port=settings.PG_PORT_ZZ,
    database=settings.PG_DATABASE_ZZ,
    user=settings.PG_USERNAME_ZZ,
    password=settings.PG_PASSWORD_ZZ,
)


################
# place all entities below
################

# Required, 必须有值的字段, 值不能是null或者''
# Optional, 值可以为null或者''的字段, 字符字段默认为'', 其它字段默认为null
class AlgTask(db.Entity):
    _table_ = 'algorithm_task'

    id = orm.PrimaryKey(str)
    task_id = orm.Optional(str)
    task_type = orm.Optional(str)
    status = orm.Optional(str)
    case_id = orm.Optional(str)
    biz_id = orm.Optional(str)
    req_param = orm.Optional(str)
    resp_param = orm.Optional(str)
    msg = orm.Optional(str)
    ip = orm.Optional(str)
    create_by = orm.Required(str)
    create_time = orm.Required(datetime, volatile=True, default=datetime.now())
    update_by = orm.Optional(str)
    update_time = orm.Optional(datetime, volatile=True, default=datetime.now())
    deleted = orm.Required(str)
    has_detail = orm.Optional(str)
    map_data = orm.Optional(str)


class CaseInfo(db.Entity):
    _table_ = 'case_info'

    id = orm.PrimaryKey(str)
    create_time = orm.Required(datetime)
    create_by = orm.Required(str)
    deleted = orm.Required(int)

    lot_id = orm.Optional(str)
    organ_id = orm.Optional(str)
    status = orm.Optional(int)
    ticket_id = orm.Optional(str)


class CaseApply(db.Entity):
    _table_ = 'case_apply'

    id = orm.PrimaryKey(str)
    case_id = orm.Required(str)

    create_time = orm.Required(datetime)
    create_by = orm.Optional(str)
    deleted = orm.Required(int)

    apply_name = orm.Optional(str)
    acceptanc_num = orm.Optional(str)  # 受理编号
    bill_count = orm.Optional(int)


class CaseBill(db.Entity):
    _table_ = 'case_bill'

    id = orm.PrimaryKey(str)
    create_time = orm.Required(datetime)
    create_by = orm.Required(str)
    deleted = orm.Required(int)

    bill_payer = orm.Optional(str)
    bill_number = orm.Optional(str)
    apply_id = orm.Optional(str)
    image_id = orm.Optional(str)
    electronic = orm.Optional(str)
    bill_type = orm.Optional(str)
    medical_date = orm.Optional(datetime)
    in_hospital_date = orm.Optional(datetime)
    out_hospital_date = orm.Optional(datetime)
    hospital_name = orm.Optional(str)
    department_name = orm.Optional(str)
    department_name_standard = orm.Optional(str)
    chief_diagnosis_name = orm.Optional(str)
    chief_diagnosis_name_standard = orm.Optional(str)
    diagnosis_name_one = orm.Optional(str)
    diagnosis_name_one_standard = orm.Optional(str)
    diagnosis_name_two = orm.Optional(str)
    diagnosis_name_two_standard = orm.Optional(str)
    diagnosis_name_three = orm.Optional(str)
    diagnosis_name_three_standard = orm.Optional(str)
    total_amount = orm.Optional(str)


class CaseDetail(db.Entity):
    _table_ = 'case_detail'

    id = orm.PrimaryKey(str)
    bill_id = orm.Required(str)
    create_time = orm.Required(datetime)
    create_by = orm.Required(str)
    deleted = orm.Required(int)

    major_item = orm.Optional(str)
    cost_name = orm.Optional(str)
    cost_name_standard = orm.Optional(str)
    amount = orm.Optional(str)


class CaseImage(db.Entity):
    _table_ = 'case_image'

    id = orm.PrimaryKey(str)
    original_name = orm.Required(str)
    orginal_path = orm.Required(str)
    thumbnail_path = orm.Required(str)
    create_time = orm.Required(datetime)
    create_by = orm.Required(str)
    deleted = orm.Required(int)

    case_id = orm.Optional(str)


class CaseApplyHistory(db.Entity):
    _table_ = 'case_apply_history'

    id = orm.PrimaryKey(str)
    create_time = orm.Required(datetime)
    create_by = orm.Required(str)
    # deleted = orm.Required(int)
    # input_version = orm.Required(str)

    case_id = orm.Optional(str)
    apply_id = orm.Optional(str)

    input_status = orm.Optional(int)


class Lot(db.Entity):
    _table_ = 'lot'

    id = orm.PrimaryKey(str)
    case_count = orm.Required(int)
    storage_path = orm.Required(str)
    base_path = orm.Required(str)
    status = orm.Required(int)
    create_time = orm.Required(datetime)
    create_by = orm.Required(str)
    deleted = orm.Required(int)

    end_time = orm.Optional(datetime)
    come_time = orm.Optional(datetime)
    accept_num = orm.Optional(str)  # 受理编号


class ConfigCalendar(db.Entity):
    _table_ = 'config_calendar'

    year = orm.Required(int)
    month = orm.Required(int)
    day = orm.Required(int)
    date = orm.Required(datetime)
    type = orm.Required(int)  # 类型(0：工作日，1：休息日)
    deleted = orm.Required(int)


class AuthUser(db.Entity):
    _table_ = 'auth_user'

    id = orm.PrimaryKey(str)
    user_name = orm.Optional(str)
    status = orm.Optional(int)
    team_id = orm.Required(int)


class AuthTeam(db.Entity):
    _table_ = 'auth_team'

    id = orm.PrimaryKey(str)
    team_name = orm.Optional(str)
    status = orm.Optional(int)


class CaseLog(db.Entity):
    _table_ = 'case_log'

    id = orm.PrimaryKey(str)
    case_id = orm.Required(str)
    # node_info = orm.Required(str)
    # case_node = orm.Required(str)
    create_by = orm.Required(str)
    create_time = orm.Required(datetime)
    deleted = orm.Required(int)


class CaseBillHistory(db.Entity):
    _table_ = 'case_bill_history'

    id = orm.PrimaryKey(str)
    create_time = orm.Required(datetime)
    create_by = orm.Required(str)
    # deleted = orm.Required(int)
    # input_version = orm.Required(str)

    case_id = orm.Optional(str)
    apply_id = orm.Optional(str)
    bill_id = orm.Optional(str)

    input_status = orm.Optional(int)


################
# place all entities above
################


# all entities connected to the database must be defined before calling generate_mapping()
# if the tables do not already exist, then they will be created
db.generate_mapping()
